from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
import os
import time

from selenium.webdriver.common.by import By

# 使用a接收返回值
a = os.popen(""""C:\Program Files\Google\Chrome\Application\chrome.exe" --remote-debugging-port=9222""")

options = Options()
options.add_experimental_option('debuggerAddress', '127.0.0.1:9222')

service = Service('D:/chromedriver-win64/chromedriver.exe')
driver = webdriver.Chrome(service=service,options=options)
# 1688商品详情页面的URL
url = 'https://www.alibaba.com/product-detail/20-Years-GPS-manufacturer-COBAN-factory_1601097441698.html?spm=a2700.galleryofferlist.topad_classic.d_image.146d2b3aSh41nq'
# 访问页面
driver.get(url)
# 设置隐式等待时间为10秒
driver.implicitly_wait(10)
print(driver.title)

#滚动滚动条
for y in range(8):
    js = 'window.scrollBy(0,200)'
    driver.execute_script(js)
    time.sleep(0.5)

#获取变现分析数据
analysis = driver.find_element(By.ID,"product-analysis")
print(analysis.text)
action = driver.find_element(By.CLASS_NAME,"introduce-action")
action.click()
iframe = driver.find_element(By.CSS_SELECTOR,".grid-scroller>iframe ")

#进入iframe
driver.switch_to.frame(iframe)
# 操作
data = driver.find_element(By.CLASS_NAME,'product-detail-data')
print(data.text)
# 跳出 iframe
driver.switch_to.default_content()

driver.quit()